package javaNIO1.regularExpressions;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with Intel IDEA.

 * @Auther: gjt
 * @Date: 2024/7/28 15:49
 */
public class SimpleGrep {
    public static void main(String[] args) throws IOException {
        // 匹配文件中含有 z 的行
        Pattern compile = Pattern.compile("z");
        // 为模式制作一个Matcher对象
        Matcher matcher = compile.matcher("");
        for (int i = 0; i < args.length; i++) {
            String fileName = args[i];
            BufferedReader br = null;
            String line ;
            try {
                 br = new BufferedReader(new FileReader(fileName));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }

            while ((line = br.readLine()) != null){
                // 将匹配器复位 ，查看当前实参行（字符串）
                matcher.reset(line);

                // 匹配到时输出
                if(matcher.find()){
                    System.out.println(fileName + ": " + line + "匹配到了");
                }
            }
            br.close();
        }
    }
}
